package org.springblade.modules.platform.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import org.springblade.modules.platform.entity.*;

import org.springblade.modules.platform.props.HuatengProperties;
import org.springblade.modules.platform.service.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.platform.utils.TimeUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;


@RestController
@AllArgsConstructor
@RequestMapping("/dataservice")
@Api(tags="对外接口API",description = "对外接口")
@Slf4j
public class DataServiceController {
    private final IDeviceTableService iDeviceTableService;
    private final IMeasuringPointTableService iMeasuringPointTableService;
    private final IPhysicalValueTableService iPhysicalValueTableService;
    private final IEngineerValueTableService iEngineerValueTableService;
    private final static RestTemplate restTemplate = new RestTemplate();
    private final HuatengProperties huatengProperties;
    private final IMeasuringChainTableService iMeasuringChainTableService;
    private final IMeasuringChainPlatformTypeService iMeasuringChainPlatformTypeService;
    private final IMeasuringChainPlatformInfoService iMeasuringChainPlatformInfoService;
    private final IMeasuringPlatformTypeService iMeasuringPlatformTypeService;

    /**
     * Authorization
     * @return
     */
    @GetMapping("/online-system-status")
    @ApiOperation(value = "在线状态查询", notes = "")
    public R<JSONObject> getOnlineSystemStatus() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",200);
        jsonObject.put("OnlineStatus",1);
        return R.data(jsonObject);
    }
    /**
     * Authorization
     * @return
     */
    @GetMapping ("/online-device-status")
    @ApiOperation(value = "设备工作在线状态查询", notes = "")
    public JSONObject getOnlineDeviceStatus( @RequestParam(value = "deviceList",required = false) String deviceLists) {
        List<DeviceTable> list = iDeviceTableService.list(Wrappers.<DeviceTable>query().lambda().in(DeviceTable::getDeviceId, deviceLists.split(",")).eq(DeviceTable::getIsDeleted, 0));
        if(Func.isEmpty(list)){
           return null;
        }
        List<String> collect = list.stream().map(DeviceTable::getDeviceChannelId).collect(Collectors.toList());
        String join = Func.join(collect);
        String forObject = restTemplate.getForObject(huatengProperties.getOutUrl() + "/dsm/onlineDeviceStatus?deviceList=" + join, String.class);
        JSONObject jsonObject = JSONObject.parseObject(forObject);
        return jsonObject;
    }
    @PostMapping("/device-realtime-collection")
    @ApiOperation(value = "设备实时采集结果查询", notes = "")
    public JSONObject getRealtime(@RequestBody List<String>  deviceLists) {
        List<DeviceTable> list = iDeviceTableService.list(Wrappers.<DeviceTable>query().lambda().in(DeviceTable::getDeviceId, deviceLists).eq(DeviceTable::getIsDeleted, 0));
        if(Func.isEmpty(list)){
            return null;
        }
        List<String> collect = list.stream().map(DeviceTable::getDeviceChannelId).collect(Collectors.toList());
        String join = Func.join(collect);
        String forObject = restTemplate.getForObject(huatengProperties.getOutUrl() + "/dsm/deviceRealtimeCollection?deviceList=" + join, String.class);
        System.out.println(forObject);
        JSONObject jsonObject = JSONObject.parseObject(forObject);
        return jsonObject;
    }


    @PostMapping("/meapointcode-realtime-collection")
    @ApiOperation(value = "测点编号实时采集结果查询", notes = "")
    public JSONObject getMeapointcode(@ApiIgnore @RequestHeader(name = "Authorization", required = false) String Authorization,
                                  @RequestBody List<String> codeList) {
        JSONObject jsonObject = new JSONObject();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Basic c3dvcmQ6c3dvcmRfc2VjcmV0");
        headers.set("Blade-Auth",Authorization);
        HttpEntity<List<String>>  httpEntity = new HttpEntity<>(codeList,headers);
        String s = restTemplate.postForObject("", httpEntity, String.class);
        jsonObject.put("code",200);
        JSONObject jsonObject1 = JSONObject.parseObject(s);
        Object data = jsonObject1.get("data");
        jsonObject.put("data",data);
        return jsonObject;
    }



    @GetMapping("/device-system-info")
    @ApiOperation(value = "设备硬件资源查询", notes = "")
    public JSONObject getDeviceInfo(String deviceList) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",200);
        List<DeviceTable> list = iDeviceTableService.list(Wrappers.<DeviceTable>query().lambda().in(DeviceTable::getDeviceId,deviceList.split(","))
                .eq(DeviceTable::getIsDeleted, 0));
        if(!CollectionUtils.isEmpty(list)){
            List<JSONObject> collect = list.stream().map(x -> {
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("DeviceNo", x.getDeviceId());
                jsonObject1.put("DeviceFactory", "华腾");
                jsonObject1.put("FirmwareVersion", "V1.0");
                jsonObject1.put("ProductionDate", "2022-08-10");
                return jsonObject1;
            }).collect(Collectors.toList());

            jsonObject.put("data",collect);
        }
        return jsonObject;
    }

    @PostMapping("/device-working-status")
    @ApiOperation(value = "当前状态查询结果", notes = "")
    public JSONObject getDeviceWorking(@RequestBody List<String> deviceList) {
        List<DeviceTable> list = iDeviceTableService.list(Wrappers.<DeviceTable>query().lambda().in(DeviceTable::getDeviceId, deviceList).eq(DeviceTable::getIsDeleted, 0));
        if(Func.isEmpty(list)){
            return null;
        }
        List<String> collect = list.stream().map(DeviceTable::getDeviceChannelId).collect(Collectors.toList());
        String join = Func.join(collect);
        JSONObject forObject = restTemplate.getForObject(huatengProperties.getOutUrl() + "/dsm/deviceWorkingStatus?deviceList=" + join, JSONObject.class);
        return forObject;
    }

    @GetMapping("/device-working-log")
    @ApiOperation(value = "采集日志查询", notes = "")
    public JSONObject getDeviceWorkingLog(@RequestBody List<String> deviceList) {
        JSONObject jsonObject = new JSONObject();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Basic c3dvcmQ6c3dvcmRfc2VjcmV0");
       // headers.set("Blade-Auth",Authorization);
        HttpEntity<List<String>>  httpEntity = new HttpEntity<>(deviceList,headers);
        ResponseEntity<String> exchange = restTemplate.exchange("", HttpMethod.GET, httpEntity, String.class);
        String body = exchange.getBody();

        jsonObject.put("code",200);
        JSONObject jsonObject1 = JSONObject.parseObject(body);
        Object data = jsonObject1.get("data");
        jsonObject.put("data",data);
        return jsonObject;
    }


    @GetMapping("/all-device-system-info")
    @ApiOperation(value = "采集设备列表查询结果", notes = "")
    public JSONObject getAllDeviceWorkingInfo() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",200);
        List<MeasuringChainTable> list = iMeasuringChainTableService.list(new QueryWrapper<MeasuringChainTable>().lambda()
                .eq(MeasuringChainTable::getChainCategory, 2)
                .eq(MeasuringChainTable::getIsDeleted, 0));
        JSONArray jsonArray = new JSONArray();

        list.forEach(x->{
            List<MeasuringChainPlatformType> list2 = iMeasuringChainPlatformTypeService.list(Wrappers.<MeasuringChainPlatformType>query().lambda().eq(MeasuringChainPlatformType::getMeasuringChainId, x.getId()));
            if (Func.isEmpty(list2)){
                return;
            }
            MeasuringChainPlatformType measuringChainPlatformType = list2.get(0);
            List<MeasuringChainPlatformInfo> list1 = iMeasuringChainPlatformInfoService.list(Wrappers.<MeasuringChainPlatformInfo>query().lambda().eq(MeasuringChainPlatformInfo::getMeasuringTypeId, measuringChainPlatformType.getId()));
            if (Func.isEmpty(list1)){
                return;
            }
            List<Long> collect = list1.stream().map(MeasuringChainPlatformInfo::getMeasuringPlatformId).collect(toList());
            List<MeasuringPlatformType> list3 = iMeasuringPlatformTypeService.list(Wrappers.<MeasuringPlatformType>query().lambda().in(MeasuringPlatformType::getId, collect));
            if (Func.isEmpty(list3)){
                return;
            }
            list3.forEach(l->{
                if(l.getDeviceChannelId().contains(",")){
                    String[] split = l.getDeviceChannelId().split(",");

                }else {
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.put("DeviceNo",l.getDeviceId());
                    jsonObject1.put("DeviceFactory","BGK");
                    jsonObject1.put("DeviceStation",x.getChainName());
                    jsonObject1.put("ChannelNum",l.getDeviceChannelId());
                    jsonObject1.put("Identification","");
                    JSONArray meaPointList = new JSONArray();
                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.put("MeaPointCode",l.getId());
                    meaPointList.add(jsonObject2);
                    jsonObject1.put("MeaPointList",meaPointList);
                    jsonArray.add(jsonObject1);
                }

            });

        });
        jsonObject.put("data",jsonArray);
        return jsonObject;
    }


    @GetMapping("/meapointtype-list")
    @ApiOperation(value = "测点类型查询", notes = "")
    public JSONObject getMeapointtypeList(@ApiIgnore @RequestHeader(name = "Authorization", required = false) String Authorization) {
        JSONObject jsonObject = new JSONObject();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Basic c3dvcmQ6c3dvcmRfc2VjcmV0");
        headers.set("Blade-Auth",Authorization);
        HttpEntity<List<String>>  httpEntity = new HttpEntity<>(null,headers);
        ResponseEntity<String> exchange = restTemplate.exchange("", HttpMethod.GET, httpEntity, String.class);
        String body = exchange.getBody();

        jsonObject.put("code",200);
        JSONObject jsonObject1 = JSONObject.parseObject(body);
        Object data = jsonObject1.get("data");
        jsonObject.put("data",data);
        return jsonObject;
    }

    @GetMapping("/meapoint-info-list")
    @ApiOperation(value = "测点信息查询", notes = "")
    public JSONObject getMeapointInfoList(@ApiIgnore @RequestHeader(name = "Authorization", required = false) String Authorization,
                                          @RequestParam(value = "MeaPointCode",required = false) String MeaPointCode,
                                          @RequestParam(value = "MeaPointType",required = false) String MeaPointType) {
        JSONObject jsonObject = new JSONObject();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Basic c3dvcmQ6c3dvcmRfc2VjcmV0");
        headers.set("Blade-Auth",Authorization);
        HttpEntity<List<String>>  httpEntity = new HttpEntity<>(null,headers);
        ResponseEntity<String> exchange = restTemplate.exchange("", HttpMethod.GET, httpEntity, String.class);
        String body = exchange.getBody();

        jsonObject.put("code",200);
        JSONObject jsonObject1 = JSONObject.parseObject(body);
        Object data = jsonObject1.get("data");
        jsonObject.put("data",data);
        return jsonObject;
    }

    @GetMapping("/meapoint-formula-list")
    @ApiOperation(value = "测点计算公式查询", notes = "")
    public JSONObject getMeapointFormulaList(@ApiIgnore @RequestHeader(name = "Authorization", required = false) String Authorization,
                                          @RequestParam(value = "MeaPointCode",required = false) String MeaPointCode) {
        JSONObject jsonObject = new JSONObject();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Basic c3dvcmQ6c3dvcmRfc2VjcmV0");
        headers.set("Blade-Auth",Authorization);
        HttpEntity<List<String>>  httpEntity = new HttpEntity<>(null,headers);
        ResponseEntity<String> exchange = restTemplate.exchange("", HttpMethod.GET, httpEntity, String.class);
        String body = exchange.getBody();

        jsonObject.put("code",200);
        JSONObject jsonObject1 = JSONObject.parseObject(body);
        Object data = jsonObject1.get("data");
        jsonObject.put("data",data);
        return jsonObject;
    }


    @GetMapping("/meadata-list")
    @ApiOperation(value = "历史监测数据查询结果", notes = "")
    public JSONObject getMeapointList(@RequestParam(value = "MeaPointCode") String MeaPointCode,
                                      @RequestParam(value = "StartTime",required = false) String StartTime,
                                      @RequestParam(value = "EndTime",required = false) String EndTime) {
        JSONObject jsonObject = new JSONObject();
        QueryWrapper<MeasuringPointTable> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isBlank(StartTime)&&!StringUtils.isBlank(EndTime)){
            long l = TimeUtils.TimeOfStrToLong(StartTime, "yyyy-MM-dd HH:mm:ss");
            long l1 = TimeUtils.TimeOfStrToLong(EndTime, "yyyy-MM-dd HH:mm:ss");
            queryWrapper.lambda().between(MeasuringPointTable::getGatherTime,l,l1);
        }
        queryWrapper.lambda().eq(MeasuringPointTable::getDeptId,MeaPointCode);
        List<MeasuringPointTable> list1 = iMeasuringPointTableService.list(queryWrapper);
        List<JSONObject> jsonObjects = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list1)){
            MeasuringPointTable measuringPointTable = list1.get(0);
            Integer benchTypeCode = measuringPointTable.getBenchTypeCode();
            Map<Long, List<MeasuringPointTable>> collect = list1.parallelStream().collect(Collectors.groupingBy(MeasuringPointTable::getGatherTime));

            if(benchTypeCode==1){
                collect.forEach((x,y)->{
                    JSONObject jsonObject1 = new JSONObject();
                    JSONObject BasicInfo = new JSONObject();
                    BasicInfo.put("MeaPointCode", MeaPointCode);
                    jsonObject1.put("BasicInfo", BasicInfo);
                    jsonObject1.put("Data", Lists.newArrayList(getBasicsMeaPoint(x,y,MeaPointCode)));
                    jsonObjects.add(jsonObject1);
                });

            }else {
                collect.forEach((x,y)->{
                    JSONObject jsonObject1 = new JSONObject();
                    JSONObject BasicInfo = new JSONObject();
                    BasicInfo.put("MeaPointCode", MeaPointCode);
                    jsonObject1.put("BasicInfo", BasicInfo);
                    jsonObject1.put("Data", Lists.newArrayList(getOneMeaPoint(x,y,MeaPointCode)));
                    jsonObjects.add(jsonObject1);
                });
            }
        }
        jsonObject.put("data",jsonObjects);
        jsonObject.put("code",200);



        return jsonObject;
    }
    public static String getFirstEightTimestamp() {
        long timestamp = System.currentTimeMillis();
        String strTimestamp = Long.toString(timestamp);
        return strTimestamp.substring(0, 8);
    }

    private JSONObject getBasicsMeaPoint(Long x, List<MeasuringPointTable> y,String MeaPointCode){
        String s = TimeUtils.timeStampConversionTime(x, "yyyy-MM-dd HH:mm:ss");
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("MeaTime",s);
        JSONObject OriginData = new JSONObject();
        Map<String, List<MeasuringPointTable>> collect = y.parallelStream().collect(Collectors.groupingBy(MeasuringPointTable::getDeviceId));

        collect.forEach((l,l1)->{
            DeviceTable deviceTable = iDeviceTableService.getOne(Wrappers.<DeviceTable>query().lambda().in(DeviceTable::getDeviceId, l)
                    .eq(DeviceTable::getIsDeleted, 0));
            if(deviceTable.getProductId().equalsIgnoreCase("HT-PERPENDICULOMETER")){//华腾垂线仪

                Optional<MeasuringPointTable> ccd1_y_after = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("ccd1_y_after")).findFirst();
                if(ccd1_y_after.isPresent()){
                    MeasuringPointTable l2 = ccd1_y_after.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","Pixel");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue1", o);
                }

                Optional<MeasuringPointTable> ccd1_y_front = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("ccd1_y_front")).findFirst();
                if(ccd1_y_front.isPresent()){
                    MeasuringPointTable l2 = ccd1_y_front.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","Pixel");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue2", o);
                }
                Optional<MeasuringPointTable> ccd3_x_after = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("ccd3_x_after")).findFirst();
                if(ccd3_x_after.isPresent()){
                    MeasuringPointTable l2 = ccd3_x_after.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","Pixel");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue3", o);
                }
                Optional<MeasuringPointTable> ccd3_x_front = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("ccd3_x_front")).findFirst();
                if(ccd3_x_front.isPresent()){
                    MeasuringPointTable l2 = ccd3_x_front.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","Pixel");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue4", o);
                }
                Optional<MeasuringPointTable> electricity = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("electricity")).findFirst();
                if(electricity.isPresent()){
                    MeasuringPointTable l2 = electricity.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","mA");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue5", o);
                }
                Optional<MeasuringPointTable> temperature = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("temperature")).findFirst();
                if(temperature.isPresent()){
                    MeasuringPointTable l2 = temperature.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","℃");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue6", o);
                }
            }else if(deviceTable.getProductId().equalsIgnoreCase("HT-DOUBLE-MARKER")){//华腾双标仪

                Optional<MeasuringPointTable> ccd3_z_after = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("ccd3_z_after")).findFirst();
                if(ccd3_z_after.isPresent()){
                    MeasuringPointTable l2 = ccd3_z_after.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","Pixel");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue7", o);
                }
                Optional<MeasuringPointTable> ccd3_z_front = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("ccd3_z_front")).findFirst();
                if(ccd3_z_front.isPresent()){
                    MeasuringPointTable l2 = ccd3_z_front.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","Pixel");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue8", o);
                }
                Optional<MeasuringPointTable> electricity = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("electricity")).findFirst();
                if(electricity.isPresent()){
                    MeasuringPointTable l2 = electricity.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","mA");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue9", o);
                }
                Optional<MeasuringPointTable> temperature = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("temperature")).findFirst();
                if(temperature.isPresent()){
                    MeasuringPointTable l2 = temperature.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","℃");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue10", o);
                }

            }else{//华腾绕角仪
                Optional<MeasuringPointTable> ccd3_z_after = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("ccd3_z_after")).findFirst();
                if(ccd3_z_after.isPresent()){
                    MeasuringPointTable l2 = ccd3_z_after.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","Pixel");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue11", o);
                }
                Optional<MeasuringPointTable> ccd3_z_front = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("ccd3_z_front")).findFirst();
                if(ccd3_z_front.isPresent()){
                    MeasuringPointTable l2 = ccd3_z_front.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","Pixel");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue12", o);
                }
                Optional<MeasuringPointTable> electricity = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("electricity")).findFirst();
                if(electricity.isPresent()){
                    MeasuringPointTable l2 = electricity.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","mA");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue13", o);
                }
                Optional<MeasuringPointTable> temperature = l1.stream().filter(l2 -> l2.getPropertieId().equalsIgnoreCase("temperature")).findFirst();
                if(temperature.isPresent()){
                    MeasuringPointTable l2 = temperature.get();
                    JSONObject o = new JSONObject();
                    o.put("Unit","℃");
                    o.put("Value",l2.getOnceValue());
                    OriginData.put("MeaValue14", o);
                }
            }
            jsonObject3.put("OriginData",OriginData);
        });
        JSONObject ResultData = new JSONObject();
        PhysicalValueTable one = iPhysicalValueTableService.getOne(Wrappers.<PhysicalValueTable>query().lambda()
                .eq(PhysicalValueTable::getGatherTime, x)
                .eq(PhysicalValueTable::getDeptId, MeaPointCode));
        if(one!=null){

            JSONObject nx = new JSONObject();
            nx.put("Unit","mm");
            nx.put("Value",one.getnX());
            ResultData.put("ResValue1", nx);


            JSONObject nz = new JSONObject();
            nz.put("Unit","mm");
            nz.put("Value",one.getnZ());
            ResultData.put("ResValue2", nz);

            JSONObject zx = new JSONObject();
            zx.put("Unit","mm");
            zx.put("Value",one.getzX());
            ResultData.put("ResValue3", zx);

            JSONObject zz = new JSONObject();
            zz.put("Unit","mm");
            zz.put("Value",one.getzZ());
            ResultData.put("ResValue4", zz);


            JSONObject deltaY = new JSONObject();
            deltaY.put("Unit","mm");
            deltaY.put("Value",one.getDeltaY());
            ResultData.put("ResValue5", deltaY);


            JSONObject dipX = new JSONObject();
            dipX.put("Unit","rad");
            dipX.put("Value",one.getDipX());
            ResultData.put("ResValue6", dipX);

            JSONObject dipY = new JSONObject();
            dipY.put("Unit","rad");
            dipY.put("Value",one.getDipY());
            ResultData.put("ResValue7", dipY);

        }
        EngineerValueTable one1 = iEngineerValueTableService.getOne(Wrappers.<EngineerValueTable>query().lambda()
                .eq(EngineerValueTable::getGatherTime, x)
                .eq(EngineerValueTable::getDeptId, MeaPointCode));
        if(one!=null){

            JSONObject x2 = new JSONObject();
            x2.put("Unit","mm");
            x2.put("Value",one1.getX());
            ResultData.put("ResValue8", x2);

            JSONObject y1 = new JSONObject();
            y1.put("Unit","mm");
            y1.put("Value",one1.getY());
            ResultData.put("ResValue9", y1);

            JSONObject z = new JSONObject();
            z.put("Unit","mm");
            z.put("Value",one1.getZ());
            ResultData.put("ResValue10", z);

            JSONObject alpha = new JSONObject();
            alpha.put("Unit","rad");
            alpha.put("Value",one1.getAlpha());
            ResultData.put("ResValue11", alpha);

            JSONObject beta = new JSONObject();
            beta.put("Unit","rad");
            beta.put("Value",one1.getBeta());
            ResultData.put("ResValue12", beta);

            JSONObject gama = new JSONObject();
            gama.put("Unit","rad");
            gama.put("Value",one1.getGama());
            ResultData.put("ResValue13", gama);
        }
        jsonObject3.put("ResultData",ResultData);
        return jsonObject3;
    }
    private JSONObject getOneMeaPoint(Long x, List<MeasuringPointTable> y,String MeaPointCode){
        String s = TimeUtils.timeStampConversionTime(x, "yyyy-MM-dd HH:mm:ss");
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("MeaTime",s);
        JSONObject OriginData = new JSONObject();
        Optional<MeasuringPointTable> first = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd4_x_after_backward")).findFirst();
        if(first.isPresent()){
            MeasuringPointTable l = first.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue1", o);
        }
        Optional<MeasuringPointTable> first2 = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd4_x_front_backward")).findFirst();
        if(first2.isPresent()){
            MeasuringPointTable l = first2.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue2", o);
        }
        Optional<MeasuringPointTable> ccd3_z_after_backward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd3_z_after_backward")).findFirst();
        if(ccd3_z_after_backward.isPresent()){
            MeasuringPointTable l = ccd3_z_after_backward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue3", o);
        }
        Optional<MeasuringPointTable> ccd3_z_front_backward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd3_z_front_backward")).findFirst();
        if(ccd3_z_front_backward.isPresent()){
            MeasuringPointTable l = ccd3_z_front_backward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue4", o);
        }
        Optional<MeasuringPointTable> ccd2_z_after_backward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd2_z_after_backward")).findFirst();
        if(ccd2_z_after_backward.isPresent()){
            MeasuringPointTable l = ccd2_z_after_backward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue5", o);
        }
        Optional<MeasuringPointTable> ccd2_z_front_backward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd2_z_front_backward")).findFirst();
        if(ccd2_z_front_backward.isPresent()){
            MeasuringPointTable l = ccd2_z_front_backward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue6", o);
        }
        Optional<MeasuringPointTable> ccd1_x_after_backward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd1_x_after_backward")).findFirst();
        if(ccd1_x_after_backward.isPresent()){
            MeasuringPointTable l = ccd1_x_after_backward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue7", o);
        }
        Optional<MeasuringPointTable> ccd1_x_front_backward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd1_x_front_backward")).findFirst();
        if(ccd1_x_front_backward.isPresent()){
            MeasuringPointTable l = ccd1_x_front_backward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue8", o);
        }
        Optional<MeasuringPointTable> ccd4_x_after_forward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd4_x_after_forward")).findFirst();
        if(ccd4_x_after_forward.isPresent()){
            MeasuringPointTable l = ccd4_x_after_forward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue9", o);
        }
        Optional<MeasuringPointTable> ccd4_x_front_forward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd4_x_front_forward")).findFirst();
        if(ccd4_x_front_forward.isPresent()){
            MeasuringPointTable l = ccd4_x_front_forward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue10", o);
        }
        Optional<MeasuringPointTable> ccd3_z_after_forward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd3_z_after_forward")).findFirst();
        if(ccd3_z_after_forward.isPresent()){
            MeasuringPointTable l = ccd3_z_after_forward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue11", o);
        }
        Optional<MeasuringPointTable> ccd3_z_front_forward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd3_z_front_forward")).findFirst();
        if(ccd3_z_front_forward.isPresent()){
            MeasuringPointTable l = ccd3_z_front_forward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue12", o);
        }
        Optional<MeasuringPointTable> ccd2_z_after_forward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd2_z_after_forward")).findFirst();
        if(ccd2_z_after_forward.isPresent()){
            MeasuringPointTable l = ccd2_z_after_forward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue13", o);
        }
        Optional<MeasuringPointTable> ccd2_z_front_forward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd2_z_front_forward")).findFirst();
        if(ccd2_z_front_forward.isPresent()){
            MeasuringPointTable l = ccd2_z_front_forward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue14", o);
        }
        Optional<MeasuringPointTable> ccd1_x_after_forward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd1_x_after_forward")).findFirst();
        if(ccd1_x_after_forward.isPresent()){
            MeasuringPointTable l = ccd1_x_after_forward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue15", o);
        }
        Optional<MeasuringPointTable> ccd1_x_front_forward = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd1_x_front_forward")).findFirst();
        if(ccd1_x_front_forward.isPresent()){
            MeasuringPointTable l = ccd1_x_front_forward.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue16", o);
        }
        Optional<MeasuringPointTable> ccd2_after_dip_y = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd2_after_dip_y")).findFirst();
        if(ccd2_after_dip_y.isPresent()){
            MeasuringPointTable l = ccd2_after_dip_y.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue17", o);
        }
        Optional<MeasuringPointTable> ccd2_front_dip_y = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd2_front_dip_y")).findFirst();
        if(ccd2_front_dip_y.isPresent()){
            MeasuringPointTable l = ccd2_front_dip_y.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue18", o);
        }

        Optional<MeasuringPointTable> ccd1_after_dip_x = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd1_after_dip_x")).findFirst();
        if(ccd1_after_dip_x.isPresent()){
            MeasuringPointTable l = ccd1_after_dip_x.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue19", o);
        }
        Optional<MeasuringPointTable> ccd1_front_dip_x = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd1_front_dip_x")).findFirst();
        if(ccd1_front_dip_x.isPresent()){
            MeasuringPointTable l = ccd1_front_dip_x.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue20", o);
        }
        Optional<MeasuringPointTable> ccd3_after_tangential_y = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd3_after_tangential_y")).findFirst();
        if(ccd3_after_tangential_y.isPresent()){
            MeasuringPointTable l = ccd3_after_tangential_y.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue21", o);
        }
        Optional<MeasuringPointTable> ccd3_front_tangential_y = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("ccd3_front_tangential_y")).findFirst();
        if(ccd3_front_tangential_y.isPresent()){
            MeasuringPointTable l = ccd3_front_tangential_y.get();
            JSONObject o = new JSONObject();
            o.put("Unit","Pixel");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue22", o);
        }

        Optional<MeasuringPointTable> electricity = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("electricity")).findFirst();
        if(electricity.isPresent()){
            MeasuringPointTable l = electricity.get();
            JSONObject o = new JSONObject();
            o.put("Unit","mA");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue23", o);
        }
        Optional<MeasuringPointTable> temperature = y.stream().filter(l -> l.getPropertieId().equalsIgnoreCase("temperature")).findFirst();
        if(temperature.isPresent()){
            MeasuringPointTable l = temperature.get();
            JSONObject o = new JSONObject();
            o.put("Unit","℃");
            o.put("Value",l.getOnceValue());
            OriginData.put("MeaValue24", o);
        }
        jsonObject3.put("OriginData",OriginData);
        JSONObject ResultData = new JSONObject();
        PhysicalValueTable one = iPhysicalValueTableService.getOne(Wrappers.<PhysicalValueTable>query().lambda()
                .eq(PhysicalValueTable::getGatherTime, x)
                .eq(PhysicalValueTable::getDeptId, MeaPointCode));
        if(one!=null){

            JSONObject nx = new JSONObject();
            nx.put("Unit","mm");
            nx.put("Value",one.getnX());
            ResultData.put("ResValue1", nx);


            JSONObject nz = new JSONObject();
            nz.put("Unit","mm");
            nz.put("Value",one.getnZ());
            ResultData.put("ResValue2", nz);

            JSONObject zx = new JSONObject();
            zx.put("Unit","mm");
            zx.put("Value",one.getzX());
            ResultData.put("ResValue3", zx);

            JSONObject zz = new JSONObject();
            zz.put("Unit","mm");
            zz.put("Value",one.getzZ());
            ResultData.put("ResValue4", zz);


            JSONObject deltaY = new JSONObject();
            deltaY.put("Unit","mm");
            deltaY.put("Value",one.getDeltaY());
            ResultData.put("ResValue5", deltaY);


            JSONObject dipX = new JSONObject();
            dipX.put("Unit","rad");
            dipX.put("Value",one.getDipX());
            ResultData.put("ResValue6", dipX);

            JSONObject dipY = new JSONObject();
            dipY.put("Unit","rad");
            dipY.put("Value",one.getDipY());
            ResultData.put("ResValue7", dipY);

        }
        EngineerValueTable one1 = iEngineerValueTableService.getOne(Wrappers.<EngineerValueTable>query().lambda()
                .eq(EngineerValueTable::getGatherTime, x)
                .eq(EngineerValueTable::getDeptId, MeaPointCode));
        if(one!=null){

            JSONObject x2 = new JSONObject();
            x2.put("Unit","mm");
            x2.put("Value",one1.getX());
            ResultData.put("ResValue8", x2);

            JSONObject y1 = new JSONObject();
            y1.put("Unit","mm");
            y1.put("Value",one1.getY());
            ResultData.put("ResValue9", y1);

            JSONObject z = new JSONObject();
            z.put("Unit","mm");
            z.put("Value",one1.getZ());
            ResultData.put("ResValue10", z);

            JSONObject alpha = new JSONObject();
            alpha.put("Unit","rad");
            alpha.put("Value",one1.getAlpha());
            ResultData.put("ResValue11", alpha);

            JSONObject beta = new JSONObject();
            beta.put("Unit","rad");
            beta.put("Value",one1.getBeta());
            ResultData.put("ResValue12", beta);

            JSONObject gama = new JSONObject();
            gama.put("Unit","rad");
            gama.put("Value",one1.getGama());
            ResultData.put("ResValue13", gama);
        }
        jsonObject3.put("ResultData",ResultData);
        return jsonObject3;
    }
}
